package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.IApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-28
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements IApAuthorService {

    /**
     * 保存作者用户
     *
     * @param apAuthor 作者信息
     * @return
     */
    @Override
    public ResponseResult<ApAuthor> saveAuthor(ApAuthor apAuthor) {
        //1.根据APP用户ID查询作者记录
        LambdaQueryWrapper<ApAuthor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApAuthor::getUserId, apAuthor.getUserId());
        int count = this.count(queryWrapper);
        //2.不存在-新增
        if (count == 0) {
            this.save(apAuthor);
        } else {
            //3.存在-更新
            this.updateById(apAuthor);
        }
        return ResponseResult.okResult(apAuthor);
    }
}
